//
//  PigRecordViewController.m
//  PigMarket
//
//  Created by mac on 2019/7/3.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PigRecordViewController.h"
#import "PigRecordCell.h"
#import "GrowRecordViewController.h"

@interface PigRecordViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 bgImg
 */
@property (nonatomic , strong) UIImageView *bgImg;
@end

@implementation PigRecordViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"档案";
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"adopt_adoptdetail_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick)];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"adopted_look_share"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick)];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#53CD54"]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}


- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.bgImg];
    [self.bgImg addSubview:self.myTableView];
}

- (void) initLayout {
    [self.bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(30, 0, 0, 0));
    }];
}

- (void) initAction {
    
}

- (void)initData {
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    @weakify(self);
    [[[PigMarketApi sharedInstance] pigRecordWithPigId:self.pigId memberId:memberId] subscribeNext:^(PigRecord * _Nullable pigRecord) {
        @strongify(self);
        [self.dataArr addObject:pigRecord];
        [self.myTableView reloadData];
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 9;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        return ScaleHeight(194);
    } else {
        return 46;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PigRecordCell *cell = [PigRecordCell cellWithTableView:tableView];
    
    if (self.dataArr.count > 0) {
        
        [cell configCellWithModel:self.dataArr[0] index:indexPath];
       
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        GrowRecordViewController *vc = [[GrowRecordViewController alloc] init];
        vc.pigId = self.pigId;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - target action

- (void)leftButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightButtonClick {
    
}
#pragma mark - private method

#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.scrollEnabled = NO;
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (UIImageView *)bgImg {
    if (!_bgImg) {
        _bgImg = [[UIImageView alloc] init];
        UIImage *img = [UIImage imageNamed:@"adopted_record_bg"];
        _bgImg.userInteractionEnabled = YES;
        //设置不被拉伸区域
        img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(100, 50, 50, 50) resizingMode:UIImageResizingModeStretch];
        _bgImg.image = img;
    }
    return _bgImg;
}
@end


